package com.gqb.PatternsDesignDynamicProxy;

import java.lang.reflect.Proxy;

public class ProxyTest {
	public static void main(String[] args) {
		PersonBean person=new PersonBeanImpl();
		PersonBean ownerProxy=getOwnerProxy(person);
		System.out.println("Name is"+ownerProxy.getName());
		try {
			ownerProxy.setHotOrNotRating(10);
		} catch (Exception e) {
			System.out.println("can not set rating from owner proxy");
		}
		System.out.println("Rating is "+ ownerProxy.getHotOrNotRating());
	}

	static PersonBean getOwnerProxy(PersonBean personBean) {

		return (PersonBean) Proxy.newProxyInstance(personBean.getClass()
				.getClassLoader(), personBean.getClass().getInterfaces(),
				new OwnerInvovationHandler(personBean));
	}
	static PersonBean getNotOwnerProxy(PersonBean personBean) {

		return (PersonBean) Proxy.newProxyInstance(personBean.getClass()
				.getClassLoader(), personBean.getClass().getInterfaces(),
				new NotOwnerInvocationHandler(personBean));
	}
}
